#ifndef BITMAP
#define BITMAP

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdint>
#include <climits>

namespace bitset
{
    template <typename T = std::string>
    struct PrintError
    {
        void printError(const T &errorMessage)
        {
            std::cout << errorMessage << std::endl;
        }
        void printMessage(const T &message)
        {
            std::cout << message << std::endl;
        }
    };

    PrintError<std::string> defaultErrorClass;

    class bitmapofUint8_t
    {
    public:
        explicit bitmapofUint8_t(size_t size) : _size(size)
        {
            if (size == 0)
            {
                _bits = nullptr;
                return;
            }
            // 最小是一，加7保证向上取整
            size_t len = (size + 7) / 8;
            _bits = new uint8_t[len];
            memset(_bits, 0, len);
        }

        // 把对应位设置为1
        void set(size_t num)
        {
            if (num >= size())
            {
                defaultErrorClass.printError("超出限制，请重新传参");
                return;
            }
            size_t i = num / 8;
            size_t j = num % 8;
            // 1左移j位，使用或运算，不需要考虑机器是大端还是小端，左移的方向就是从低字节到高字节
            _bits[i] |= (1u << j);
        }

        // 把对应位设置为0
        void reset(size_t num)
        {
            if (num >= size())
            {
                defaultErrorClass.printError("超出限制，请重新传参");
                return;
            }
            size_t i = num / 8;
            size_t j = num % 8;
            // 1左移j位，然后按位取反，使用与运算
            _bits[i] &= (~(1u << j));
        }

        bool check(size_t num)
        {
            if (_bits == nullptr || num >= size())
                return false;
            size_t i = num / 8;
            size_t j = num % 8;

            return (_bits[i] & (1u << j)) != 0;
        }

        // 只是清空数组内容，不需要释放空间
        void clear()
        {
            if (_bits == nullptr)
                return;

            size_t bit = (_size + 7) / 8;
            memset(_bits, 0, bit);
        }

        size_t size() const
        {
            return _size;
        }
        ~bitmapofUint8_t()
        {
            delete[] _bits;
        }

    private:
        uint8_t *_bits = nullptr; //
        size_t _size = 0;         // 操纵的比特位的总位数
    };

    template <size_t N>
    class bitmapofVector
    {
    public:
        explicit bitmapofVector()
        {
            _bits.resize((N + 31) / 32, 0);
        }

        void set(size_t num)
        {
            if (num > N)
            {
                defaultErrorClass.printError("超出限制，请重新传参");
                return;
            }
            size_t i = num / 32;
            size_t j = num % 32;

            _bits[i] |= (1u << j);
        }

        void reset(size_t num)
        {
            if (num >= N)
            {
                defaultErrorClass.printError("超出限制，请重新传参");
                return;
            }
            size_t i = num / 32;
            size_t j = num % 32;

            _bits[i] &= (~(1u << j));
        }

        bool check(size_t num)
        {
            if (num >= N)
            {
                defaultErrorClass.printError("超出限制，请重新传参");
                return false;
            }
            size_t i = num / 32;
            size_t j = num % 32;

            return (_bits[i] & (1u << j)) != 0;
        }

        void clear()
        {
            for (uint32_t &x : _bits)
            {
                x = 0;
            }
        }

        ~bitmapofVector() {}

    private:
        std::vector<uint32_t> _bits;
    };

}

#endif